home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / DLLSKEL.PAK / EXPORTS.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  107 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   exports.c
  9. //
  10. //  PURPOSE:   Contains the DLL's exported functions.
  11. //
  12. //  FUNCTIONS:
  13. //    DLLFunction1  -  Displays a message passed in by an application
  14. //    DLLFunction2  -  Squares a number and displays the number & its square
  15. //
  16. //  COMMENTS:
  17. //    These functions are simple and are meant to demonstrate different
  18. //    calling conventions and parameters.
  19. //
  20. //    Since DLLs cannot control what types of applications call them, they
  21. //    should be able to work properly with multithreaded applications.
  22. //    This means using critical sections and mutexs as needed.  Do not
  23. //    expect that an application will call DLL functions from within a
  24. //    critical section or mutex!
  25. //
  26.  
  27.  
  28. #include <windows.h>
  29. #include <stdio.h>
  30. #include "dllglob.h"
  31.  
  32.  
  33. // Global variable definitions
  34. CRITICAL_SECTION gCriticalSection;   // Initialized in DllMain().
  35.  
  36. float gRoot = 0.0F;
  37.  
  38.  
  39. //
  40. //  FUNCTION: DLLFunction1(HWND, LPSTR)
  41. //
  42. //  PURPOSE:
  43. //    To display a message passed in by an application.
  44. //
  45. //  PARAMETERS:
  46. //    hWnd       -  Handle of the window that is to be parent of message box
  47. //    lpszMsg    -  Character string containing a message to display.
  48. //
  49. //  RETURN VALUE:
  50. //    Always returns TRUE
  51. //
  52.  
  53. DLLEXPORT BOOL WINAPI DLLFunction1(HWND hWnd, LPSTR lpszMsg)
  54. {
  55.     MessageBox(hWnd, lpszMsg, "DLLFunction1()", MB_OK);
  56.     return TRUE;
  57. }
  58.  
  59.  
  60. //
  61. //  FUNCTION: DLLFunction2(HWND)
  62. //
  63. //  PURPOSE:
  64. //    Computes the square of a number and displays the number and its square
  65. //    in a messag box.
  66. //
  67. //  PARAMETERS:
  68. //    hWnd       -  Handle of the window that is to be parent of message box
  69. //
  70. //  RETURN VALUE:
  71. //    The square of root.
  72. //
  73. //  COMMENTS:
  74. //    This function uses a critical section to prevent simultaneous
  75. //    updates of the gRoot global variable by multiple threads.  This
  76. //    is important because the value is changed and after it is changed
  77. //    it is formatted in a string.
  78. //
  79. //    This function minimizes the time in the critical section by only
  80. //    copying the global variable into a local stack-based variable.
  81. //    The stack-based variable is then used in calculations.  Since each
  82. //    thread gets its own stack, stack-based variables are not shared
  83. //    by multiple threads and thus don't need to be put into critical
  84. //    sections.
  85. //
  86.  
  87. DLLEXPORT float _stdcall DLLFunction2(HWND hWnd)
  88. {
  89.     char  szMsg[80];
  90.     float square, localRoot;
  91.  
  92.     // Minimize time in the critical section by copying global data
  93.     // into a stack-based variable, which can't be modified by other
  94.     // threads.
  95.  
  96.     EnterCriticalSection(&gCriticalSection);
  97.     localRoot = gRoot += 1.0F;
  98.     LeaveCriticalSection(&gCriticalSection);
  99.  
  100.     square = localRoot * localRoot;
  101.  
  102.     sprintf(szMsg, "The square of %f is %f.", localRoot, square);
  103.     MessageBox(hWnd, szMsg, "DLLFunction2()", MB_OK);
  104.  
  105.     return square;
  106. }
  107.